//
//  NetWorkManager.m
//  xiaoyixiu
//
//  Created by hanzhanbing on 16/6/14.
//  Copyright © 2016年 柯南. All rights reserved.
//


#import "NetWorkManager.h"
#import <AFNetworking.h>
#import "NetWorkUtil.h"
#import "UUID.h"
#import "LoginNumberViewController.h"
#import "AppDelegate.h"
#import "DCRequestSerializer.h"
#import <乐赚兼职-Swift.h>

#define TIMEOUT 100

@interface NetWorkManager ()

@end

@implementation NetWorkManager

+ (instancetype)sharedManager
{
    static NetWorkManager *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[NetWorkManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
        _manager.completionQueue = dispatch_queue_create("afn.NetworkQueue", NULL);
        _manager.requestSerializer = [DCRequestSerializer serializer];
    });
    return _manager;
}

#pragma mark Data
- (NSURLSessionDataTask *)postData:(NSString *)name
                        parameters:(NSDictionary *)parameters
                           success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                           failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    if ([NetWorkUtil currentNetWorkStatus] == NET_UNKNOWN) {
        
        return nil;
    }
    
    AFJSONResponseSerializer *response = [[AFJSONResponseSerializer alloc] init];

    self.responseSerializer = response;
    
    self.requestSerializer = [AFJSONRequestSerializer serializer];
    
    self.requestSerializer.stringEncoding = NSUTF8StringEncoding;
    
    self.requestSerializer.timeoutInterval = TIMEOUT;
    
    [self.requestSerializer setValue:[UUID getUUIDByKeyChain] forHTTPHeaderField:@"deviceUUID"];

    
    UserModel *user = [UserModel sharedInstance:UserKey0];
    
    
//    [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
//    [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"multipart/form-data",@"application/json", @"text/json", nil];
    [self.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];

    NSString *urlStr=[NSString stringWithFormat:@"%@%@",APPUrl,name];
    
//parameters
    return [self POST:urlStr parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            
            int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
            
            if (IsSuccess == 502) { //成功
                
            }
            else {
                success(task, responseObject);
                
                
            }
            //
        }
        DLog(@"%@参数%@\n%@返回结果：%@",self.requestSerializer.HTTPRequestHeaders,parameters,urlStr,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(task, error);

        DLog(@"%@请求失败信息：%@",urlStr,[error localizedDescription]);
    }];
    
    
//    return [self POST:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//        if ([responseObject isKindOfClass:[NSDictionary class]]) {
//
//            int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
//
//            if (IsSuccess == 502) { //成功
//
//            }
//            else {
//                success(task, responseObject);
//
//
//            }
//            //
//        }
//        DLog(@"%@参数%@\n%@返回结果：%@",self.requestSerializer.HTTPRequestHeaders,parameters,urlStr,responseObject);
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//        failure(task, error);
//
//        DLog(@"%@请求失败信息：%@",urlStr,[error localizedDescription]);
//
//    }];
}

- (NSURLSessionDataTask *)getData:(NSString *)name
                       parameters:(NSDictionary *)parameters
                          success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                          failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
//    if ([NetWorkUtil currentNetWorkStatus] == NET_UNKNOWN) {
//        [MessageAlertView showErrorMessage:@"请检查您的网络"];
//        return nil;
//    }
    UserModel *model = [UserModel sharedInstance:UserKey0];
    
    self.responseSerializer=[AFHTTPResponseSerializer serializer];
//    self.requestSerializer = [AFHTTPRequestSerializer serializer];
    self.requestSerializer.stringEncoding = NSUTF8StringEncoding;
    AFJSONResponseSerializer *response = [[AFJSONResponseSerializer alloc] init];
    response.removesKeysWithNullValues = YES;
    self.responseSerializer = response;
//    [self.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    
    self.requestSerializer.timeoutInterval = TIMEOUT;
    
//    [self.requestSerializer didChangeValueForKey:@"timeoutInterval"];
  
    [self.requestSerializer setValue:[UUID getUUIDByKeyChain] forHTTPHeaderField:@"deviceUUID"];
    [self.requestSerializer setValue:model.token forHTTPHeaderField:@"token"];
        
    
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", nil];
    
//    if (![UtilTools isBlankString:Context.currentUser.token]) {
//        [self.requestSerializer setValue:Context.currentUser.token forHTTPHeaderField:@"token"];
//
//    }
    NSString *url=[NSString stringWithFormat:@"%@%@",APPUrl,name];
    
    
    
    return [self GET:url parameters:parameters headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
            if (IsSuccess == 502) { //成功
                

            }
            else {
                success(task, responseObject);

            }
        }else{
            NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            int IsSuccess = [[NSString stringWithFormat:@"%@",[resultDic objectForKey:@"code"]] intValue];
            if (IsSuccess == 502) { //成功
               
            }else {
                success(task, resultDic);

            }
        }
        
        
        
        
        DLog(@"%@接口\n%@返回结果：%@",parameters,url,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(task, error);
        DLog(@"%@请求失败信息：%@",url,[error localizedDescription]);
    }];
    
//    return [self GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//        if ([responseObject isKindOfClass:[NSDictionary class]]) {
//            int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
//            if (IsSuccess == 502) { //成功
//
//
//            }
//            else {
//                success(task, responseObject);
//
//            }
//        }else{
//            NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//            int IsSuccess = [[NSString stringWithFormat:@"%@",[resultDic objectForKey:@"code"]] intValue];
//            if (IsSuccess == 502) { //成功
//
//            }else {
//                success(task, resultDic);
//
//            }
//        }
//
//
//
//
//        DLog(@"%@接口\n%@返回结果：%@",parameters,url,responseObject);
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//        failure(task, error);
//        DLog(@"%@请求失败信息：%@",url,[error localizedDescription]);
//
//    }];
}
#pragma mark Json
- (NSURLSessionDataTask *)postJSON:(NSString *)name
                        parameters:(NSDictionary *)parameters
                           success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                           failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    if ([NetWorkUtil currentNetWorkStatus] == NET_UNKNOWN) {
        
        return nil;
    }
    
    AFJSONResponseSerializer *response = [[AFJSONResponseSerializer alloc] init];

    self.responseSerializer = response;
    
    self.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    self.requestSerializer.stringEncoding = NSUTF8StringEncoding;
    
    self.requestSerializer.timeoutInterval = TIMEOUT;
    
    [self.requestSerializer setValue:[UUID getUUIDByKeyChain] forHTTPHeaderField:@"deviceUUID"];

    
    UserModel *user = [UserModel sharedInstance:UserKey0];
    
    
//    [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
//    [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"multipart/form-data",@"application/json", @"text/json", nil];
    [self.requestSerializer setValue:user.token forHTTPHeaderField:@"token"];

    NSString *urlStr=[NSString stringWithFormat:@"%@%@",APPUrl,name];
    
//parameters
    return [self POST:urlStr parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            
            int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
            
            if (IsSuccess == 502) { //成功
                
            }
            else {
                success(task, responseObject);
                
                
            }
            //
        }
        DLog(@"%@参数%@\n%@返回结果：%@",self.requestSerializer.HTTPRequestHeaders,parameters,urlStr,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(task, error);

        DLog(@"%@请求失败信息：%@",urlStr,[error localizedDescription]);
    }];
    
//    return [self POST:urlStr parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//        if ([responseObject isKindOfClass:[NSDictionary class]]) {
//
//            int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
//
//            if (IsSuccess == 502) { //成功
//
//            }
//            else {
//                success(task, responseObject);
//
//
//            }
//            //
//        }
//        DLog(@"%@参数%@\n%@返回结果：%@",self.requestSerializer.HTTPRequestHeaders,parameters,urlStr,responseObject);
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//        failure(task, error);
//
//        DLog(@"%@请求失败信息：%@",urlStr,[error localizedDescription]);
//
//    }];
}

- (NSURLSessionDataTask *)getJSON:(NSString *)name
                       parameters:(NSDictionary *)parameters
                          success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                          failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    if ([NetWorkUtil currentNetWorkStatus] == NET_UNKNOWN) {
        [HWProgressHUD showMessage:@"当前网络不可用,请检查网络设置"];
        failure(nil,nil);
        return nil;
    }
    
    [self configNetManager1:name];
    
    
    NSString *url=[NSString stringWithFormat:@"%@%@",APPUrl,name];
    
    
    
    [NetRequest getWithUrl:url parameters:parameters seccese:^(id _Nonnull responseObject) {
        int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
        if (IsSuccess == 502) { //成功
       
            [UserModel clearData:UserKey0];
//            [[DCDBMenager shard] deleteAllFriend];
//            [[DCDBMenager shard] deleteAllStranger];
//            [[DCDBMenager shard] deleteAllGroup];
//            [[DCDBMenager shard] deleteAllGroupMember];
            //[[DCDBMenager shard] clearChatTop];
            dispatch_async(dispatch_get_main_queue(), ^{
                [[AppDelegate shareAppDelegate]setLoginViewController];
            });
        }
        else
         {
             success(nil, responseObject);
        }
    } faild:^(id _Nonnull err) {
        failure(nil,nil);
        DLog(@"%@请求失败信息：%@",url,err);
    }];
    return nil;

//    return [self GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        });
//        if ([responseObject isKindOfClass:[NSDictionary class]]) {
//
//        int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
//
//        if (IsSuccess == 502) { //成功
//
//            [[EMClient sharedClient] logout:YES];
//            [UserModel clearData:UserKey0];
//            [[DCDBMenager shard] deleteAllFriend];
//            [[DCDBMenager shard] deleteAllStranger];
//            [[DCDBMenager shard] deleteAllGroup];
//            [[DCDBMenager shard] deleteAllGroupMember];
//            [[DCDBMenager shard] clearChatTop];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                          [[AppDelegate shareAppDelegate]setLoginViewController];
//
//                      });
//        } else {
//
//            success(task, responseObject);
//
//
//        }
//          }
//DLog(@"%@接口\n%@参数%@返回结果：%@",self.requestSerializer.HTTPRequestHeaders,parameters,url,responseObject);
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        });
//        failure(task, error);
//        DLog(@"%@请求失败信息：%@",url,[error localizedDescription]);
//    }];
}

- (NSURLSessionDataTask *)postNoTipJSON:(NSString *)name
                             parameters:(NSDictionary *)parameters
                                success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                                failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    if ([NetWorkUtil currentNetWorkStatus] == NET_UNKNOWN) {

        return nil;
    }
    
    [self configNetManager:name];
    
    NSString *url=[NSString stringWithFormat:@"%@%@",APPUrl,name];
//    [NetRequest postWithUrl:url parameters:parameters seccese:^(id _Nonnull responseObject) {
//        int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
//        if (IsSuccess == 502) { //成功
//            [[EMClient sharedClient] logout:YES];
//            [UserModel clearData:UserKey0];
//            [[DCDBMenager shard] deleteAllFriend];
//            [[DCDBMenager shard] deleteAllStranger];
//            [[DCDBMenager shard] deleteAllGroup];
//            [[DCDBMenager shard] deleteAllGroupMember];
//            [[DCDBMenager shard] clearChatTop];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                          [[AppDelegate shareAppDelegate]setLoginViewController];
//
//                      });        }
//        else
//         {
//             success(nil, responseObject);
//        }
//    } faild:^(id _Nonnull err) {
//        failure(nil,nil);
//        DLog(@"%@请求失败信息：%@",url,err);
//    }];
    return nil;
//    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", nil];
//
//    return [self POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//        DLog(@"参数%@\n%@返回结果：%@",parameters,url,responseObject);
//
////        int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
////        if (IsSuccess == 200) { //成功
//            success(task, responseObject);
////        } else {
////
////
////            failure(task, nil);
////        }
////
//
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//        failure(task, error);
//        DLog(@"%@请求失败信息：%@",url,[error localizedDescription]);
//
//    }];
}

- (NSURLSessionDataTask *)getNoTipJSON:(NSString *)name
                            parameters:(NSDictionary *)parameters
                               success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                               failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    if ([NetWorkUtil currentNetWorkStatus] == NET_UNKNOWN) {
        

        return nil;
    }
    
    [self configNetManager:name];
    
    
    NSString *url=[NSString stringWithFormat:@"%@%@",APPUrl,name];
//    [NetRequest postWithUrl:url parameters:
//     parameters seccese:^(id _Nonnull responseObject) {
//        int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
//        if (IsSuccess == 502) { //成功
//            [[EMClient sharedClient] logout:YES];
//            [UserModel clearData:UserKey0];
//            [[DCDBMenager shard] deleteAllFriend];
//            [[DCDBMenager shard] deleteAllStranger];
//            [[DCDBMenager shard] deleteAllGroup];
//            [[DCDBMenager shard] deleteAllGroupMember];
//            [[DCDBMenager shard] clearChatTop];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                          [[AppDelegate shareAppDelegate]setLoginViewController];
//
//                      });        }
//        else
//         {
//             success(nil, responseObject);
//        }
//    } faild:^(id _Nonnull err) {
//        failure(nil,nil);
//        DLog(@"%@请求失败信息：%@",url,err);
//    }];
    return nil;
//    return [self GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//        if ([responseObject isKindOfClass:[NSDictionary class]]) {
//
//
//        int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
//        if (IsSuccess == 200) { //成功
//            success(task, responseObject);
//        } else {
//
//
//
//            failure(task, nil);
//        }
//        }
//        DLog(@"%@接口\n%@参数\n%@返回结果：%@",self.requestSerializer.HTTPRequestHeaders,parameters,url,responseObject);
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//        failure(task, error);
//        DLog(@"%@请求失败信息：%@",url,[error localizedDescription]);
//    }];
}
- (NSURLSessionDataTask *)postfile:(NSString *)name
                        parameters:(NSDictionary *)parameters
         constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block

                           success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                           failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
//    self.requestSerializer = [AFHTTPRequestSerializer serializer];
    self.responseSerializer=[AFHTTPResponseSerializer serializer];
//    [self.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    
    self.requestSerializer.timeoutInterval = TIMEOUT;
   
    [self.requestSerializer setValue:[UUID getUUIDByKeyChain] forHTTPHeaderField:@"deviceUUID"];
   
        [self.requestSerializer setValue:@"" forHTTPHeaderField:@"token"];
    
    
    
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", nil];
    
    
    
//    if (![UtilTools isBlankString:Context.currentUser.token]) {
//        [self.requestSerializer setValue:Context.currentUser.token forHTTPHeaderField:@"token"];
//
//    }
    name=[NSString stringWithFormat:@"%@%@",APPUrl,name];
    
    return [self postfile:name parameters:parameters constructingBodyWithBlock:block success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            DLog(@"参数%@\n%@返回结果：%@",parameters,name,responseObject);
            int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
            if (IsSuccess == 200) { //成功
                success(task, responseObject);
            } else {
                NSString *message = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"resultMsg"]];
                
                failure(task, nil);
            }
            
        }else{
        NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        DLog(@"参数%@\n%@返回结果：%@",parameters,name,resultDic);
        int IsSuccess = [[NSString stringWithFormat:@"%@",[resultDic objectForKey:@"code"]] intValue];
        if (IsSuccess == 200) { //成功
            success(task, resultDic);
        } else {
            NSString *message = [NSString stringWithFormat:@"%@",[resultDic objectForKey:@"resultMsg"]];
            
            failure(task, nil);
        }}
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failure(task, error);
    }];
    
    
    
//    return [self POST:name parameters:parameters constructingBodyWithBlock:block progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//        if ([responseObject isKindOfClass:[NSDictionary class]]) {
//            DLog(@"参数%@\n%@返回结果：%@",parameters,name,responseObject);
//            int IsSuccess = [[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"code"]] intValue];
//            if (IsSuccess == 200) { //成功
//                success(task, responseObject);
//            } else {
//                NSString *message = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"resultMsg"]];
//
//                failure(task, nil);
//            }
//
//        }else{
//        NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//
//        DLog(@"参数%@\n%@返回结果：%@",parameters,name,resultDic);
//        int IsSuccess = [[NSString stringWithFormat:@"%@",[resultDic objectForKey:@"code"]] intValue];
//        if (IsSuccess == 200) { //成功
//            success(task, resultDic);
//        } else {
//            NSString *message = [NSString stringWithFormat:@"%@",[resultDic objectForKey:@"resultMsg"]];
//
//            failure(task, nil);
//        }}
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//        failure(task, error);
//    }];
}

//配置请求头
- (void)configNetManager:(NSString *)name
{
    self.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    self.requestSerializer.stringEncoding = NSUTF8StringEncoding;
    AFJSONResponseSerializer *response = [[AFJSONResponseSerializer alloc] init];
    response.removesKeysWithNullValues = YES;
    self.responseSerializer = response;
//    [self.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    
//    self.requestSerializer.timeoutInterval = TIMEOUT;
    
//    [self.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [self.requestSerializer setValue:[UUID getUUIDByKeyChain] forHTTPHeaderField:@"deviceUUID"];
   
        [self.requestSerializer setValue:@"" forHTTPHeaderField:@"token"];

    

    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", nil];
    
//    if (![UtilTools isBlankString:Context.currentUser.token]) {
//        [self.requestSerializer setValue:Context.currentUser.token forHTTPHeaderField:@"token"];
//
//    }
}
- (void)configNetManager1:(NSString *)name
{
    self.requestSerializer = [AFJSONRequestSerializer serializer];
    self.requestSerializer.stringEncoding = NSUTF8StringEncoding;
    AFJSONResponseSerializer *response = [[AFJSONResponseSerializer alloc] init];
    response.removesKeysWithNullValues = YES;
    self.responseSerializer = response;
//    [self.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    
//    self.requestSerializer.timeoutInterval = TIMEOUT;
    
//    [self.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [self.requestSerializer setValue:[UUID getUUIDByKeyChain] forHTTPHeaderField:@"deviceUUID"];
   
//        [self.requestSerializer setValue:@"" forHTTPHeaderField:@"token"];

    

    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", nil];
    
//    if (![UtilTools isBlankString:Context.currentUser.token]) {
//        [self.requestSerializer setValue:Context.currentUser.token forHTTPHeaderField:@"token"];
//
//    }
}
-(void)getWithUrlString:(NSString *)urlString parameters:(NSDictionary *)parameters success:(HttpSuccess)success failure:(HttpFailure)failure{
  //创建请求管理者
  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
 
  manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  //内容类型
  manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
  //get请求
     urlString=[NSString stringWithFormat:@"%@%@",APPUrl,urlString];
    [manager GET:urlString parameters:parameters headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
    
//  [manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
//    //数据请求的进度
//  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//    success(responseObject);
//  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//    failure(error);
//  }];
 
}
 
//POST请求
-(void)postWithUrlString:(NSString *)urlString parameters:(NSDictionary *)parameters success:(HttpSuccess)success failure:(HttpFailure)failure{
  //创建请求管理者
  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  //
  manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  //内容类型
  manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"multipart/form-data", nil];
  //post请求
    [manager POST:urlString parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        //数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
    
    
    
//  [manager POST:urlString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
//    //数据请求的进度
//  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//    success(responseObject);
//  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//    failure(error);
//  }];
}
- (void)postJsonWithURLString:(NSString *)urlString parameters:(NSDictionary *)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure {
    
    NSDictionary *body = parameters;
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
    
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    
    NSString *baseString = [NSString stringWithFormat:@"%@%@",APPUrl,urlString];
    
    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:baseString parameters:nil error:nil];
    
    req.timeoutInterval= TIMEOUT;
    
    [req setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
//    [req setValue:@"" forHTTPHeaderField: ]
//    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
    UserModel *user = [UserModel sharedInstance:UserKey0];
    
    [req setValue:user.token forHTTPHeaderField:@"token"];
    
    [req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//    req setbody
    
    [req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    [manager dataTaskWithRequest:req uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (!error) {
            
            NSLog(@"Reply JSON: %@", responseObject);

            success(responseObject);

        } else {
            
            NSLog(@"Error: %@", error);
            failure(error);
            
        }
        
    }];
    
//    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
//
//        if (!error) {
//
//            NSLog(@"Reply JSON: %@", responseObject);
//
//            success(responseObject);
//
//        } else {
//
//            NSLog(@"Error: %@", error);
//            failure(error);
//
//        }
//
//    }] resume];
    
}
- (NSString *)convertToJsonData:(NSDictionary *)dict{

NSError *error;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

NSString *jsonString;

if (!jsonData) {

NSLog(@"%@",error);

}else{

jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

}

NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];

NSRange range = {0,jsonString.length};

//去掉字符串中的空格

[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];

NSRange range2 = {0,mutStr.length};

//去掉字符串中的换行符

[mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

//替换掉网址的\转义    \/  替换为 /

[mutStr replaceOccurrencesOfString:@"\\/" withString:@"/" options:NSLiteralSearch range:NSMakeRange(0,mutStr.length)];

return mutStr;

}
@end
